package com.topboya.linker.model;

import com.topboya.linker.model.levelitem.DepartmentGroup;
import com.topboya.linker.model.response.DeptUserResponse;
import com.topboya.linker.utils.LogUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created by Administrator on 2019/1/22 0022.
 */

public class DataManager {
    private static final String TAG = "DataManager";
     private List<DeptUserResponse.Data> mData;
     private List<DepartmentGroup> departmentGroupList;
     private Stack<List<DepartmentGroup>> stack = new Stack<>();
      public DataManager(List<DeptUserResponse.Data> mData){

          this.mData =mData;
          departmentGroupList=new ArrayList<>();
      }

      public  List<DepartmentGroup> getCompanydata(){

               departmentGroupList.clear();
          for (int i=0;i < mData.size();i++){
              List<List<DeptUserResponse.SubDeptList>>  subDeptListss = new ArrayList<>();
              List<List<DeptUserResponse.DeptUserList>>  deptUserListss = new ArrayList<>();
              DepartmentGroup  departmentGroup=new DepartmentGroup();
              List<DeptUserResponse.SubDeptList>  subDeptLists =  mData.get(i).getSubDeptList();
              List<DeptUserResponse.DeptUserList> deptUserLists = mData.get(i).getDeptUserList();
              departmentGroup.setDeparemtname(mData.get(i).getName());
              departmentGroup.setDeptUserListss(deptUserListss);
              departmentGroup.setSubDeptListss(subDeptListss);
              departmentGroup.setSub(true);
              departmentGroup.setIschecked(false);
              departmentGroupList.add(departmentGroup);
              subDeptListss.add(subDeptLists);
              deptUserListss.add(deptUserLists);

          }
          return departmentGroupList;

      }

//      用于判断遍历是否到头，如果没有孩子结点，就跳出递归
      private boolean isover =false;
    public List<DepartmentGroup> getNextLayerdata(int position, boolean isChecked){

//          if (!departmentGroupList.get(position).isSub()){
////              如果不是sub部门就不执行，直接返回，后面已经没有成员了，在执行点击事件没有意义
//              isover = true;
//              return departmentGroupList;
//          }
        if (null == departmentGroupList.get(position).getDeptUserListss()){
            isover = true;
            return departmentGroupList;
        }
          if (null==departmentGroupList.get(position).getSubDeptListss()){
              isover = true;
              return departmentGroupList;
          }

        List<DepartmentGroup> departmentGroupListstatck = new ArrayList<>();
        departmentGroupListstatck.addAll(departmentGroupList);
        stack.push(departmentGroupListstatck);
        List<List<DeptUserResponse.SubDeptList>> subDeptListss = departmentGroupList.get(position).getSubDeptListss();
        List<List<DeptUserResponse.DeptUserList>> deptUserListss = departmentGroupList.get(position).getDeptUserListss();
        LogUtils.v(TAG + "subDeptListss:"+subDeptListss.toString());
        this.departmentGroupList.clear();
        List<DeptUserResponse.SubDeptList> subDeptLists = subDeptListss.get(position);
        List<DeptUserResponse.DeptUserList> deptUserLists = deptUserListss.get(position);

        if (null !=subDeptLists){
            List<List<DeptUserResponse.SubDeptList>> subDeptListssnext = new ArrayList<>();
            List<List<DeptUserResponse.DeptUserList>> deptUserListssnext =new ArrayList<>();
            for (int i=0;i <subDeptLists.size();i++){

                DepartmentGroup  departmentGroup=new DepartmentGroup();
                departmentGroup.setDeparemtname(subDeptLists.get(i).getName());
                departmentGroup.setSub(true);
                departmentGroup.setIschecked(isChecked);

                if (null !=subDeptLists){

                    subDeptListssnext.add(subDeptLists.get(i).getSubDeptList());
                    deptUserListssnext.add(subDeptLists.get(i).getDeptUserList());
                    departmentGroup.setDeptUserListss(deptUserListssnext);
                    departmentGroup.setSubDeptListss(subDeptListssnext);
                }
                 departmentGroupList.add(departmentGroup);

            }
        }
        if (null !=deptUserLists){

            for (int i=0;i <deptUserLists.size();i++){

                DepartmentGroup  departmentGroup=new DepartmentGroup();
                departmentGroup.setDeparemtname(deptUserLists.get(i).getName());
                departmentGroup.setSub(false);
                departmentGroup.setIschecked(isChecked);
                departmentGroupList.add(departmentGroup);

            }
        }
        return  departmentGroupList;

    }

    public  List<DepartmentGroup>  getPreListData(){

//出栈
        if (stack.isEmpty()){

            return departmentGroupList;
        }
        departmentGroupList = stack.pop();

        LogUtils.v(TAG +"departmentGroupList:"+departmentGroupList.toString() );
//
           return departmentGroupList;
    }

        List<DepartmentGroup> departmentGroupsCildList = new ArrayList<>();
        List<DepartmentGroup> departmentGroupsCildListold = new ArrayList<>();
      public List<DepartmentGroup> getSelected(int position,List<DepartmentGroup> departmentGroupList){

            departmentGroupsCildListold.clear();
//          得到遍历到的所有孩子，拿到结果
            departmentGroupsCildListold.addAll(departmentGroupList);
            selectSelf(position,departmentGroupList);
            selectChild(departmentGroupList.get(position));
            this.departmentGroupList.clear();
            this.departmentGroupList.addAll(departmentGroupsCildListold);
//         LogUtils.v(TAG + "departmentGroupList-----:"+this.departmentGroupList.toString());

          return departmentGroupsCildList;
      }

     List<DepartmentGroup> departmentGroupstemp = new ArrayList<>();
    //递归遍历孩子
    private List<DepartmentGroup>  selectChild(int position ) {

        List<DepartmentGroup> dchild = getNextLayerdata1(position,true);

         if (null !=dchild ){

             LogUtils.v(TAG + "departmentGroupstemp:---->"+this.departmentGroupstemp.toString());
             departmentGroupsCildList.addAll(dchild);

             for (int i=0; i< dchild.size();i++){

                  selectChild(i);
             }
         }
        return departmentGroupsCildList;
    }


    private List<DepartmentGroup>  selectChild(DepartmentGroup group ) {

        List<DepartmentGroup>  dchild = getChild(group,true);
        if (null != dchild){

            LogUtils.v(TAG + "dchild:"+dchild.toString());
            departmentGroupsCildList.addAll(dchild);
            for (int i=0;i < dchild.size();i++){

                DepartmentGroup item = dchild.get(i);
                selectChild(item);
            }
        }
       return departmentGroupsCildList;
    }

    private List<DepartmentGroup> getChild(DepartmentGroup group, boolean b) {

              List<DepartmentGroup> result = new ArrayList<>();
              if (null ==group){

                  return result;
              }
        return  result;
    }


    //递归函数
    private void selectSelf(int position,List<DepartmentGroup> departmentGroupList ) {

        departmentGroupList.get(position).setIschecked(true);

        departmentGroupsCildList.add(departmentGroupList.get(position));

    }

    public List<DepartmentGroup> getNextLayerdata1(int position, boolean isChecked){

//          if (!departmentGroupList.get(position).isSub()){
////              如果不是sub部门就不执行，直接返回，后面已经没有成员了，在执行点击事件没有意义
//              isover = true;
//              return departmentGroupList;
//          }
        if (null == departmentGroupList.get(position).getDeptUserListss()){

            return null;
        }
        if (null==departmentGroupList.get(position).getSubDeptListss()){

            return null;
        }

        List<List<DeptUserResponse.SubDeptList>> subDeptListss = departmentGroupList.get(position).getSubDeptListss();
        List<List<DeptUserResponse.DeptUserList>> deptUserListss = departmentGroupList.get(position).getDeptUserListss();
//        LogUtils.v(TAG + "subDeptListss:"+subDeptListss.toString());
        this.departmentGroupList.clear();
        List<DeptUserResponse.SubDeptList> subDeptLists = subDeptListss.get(position);
        List<DeptUserResponse.DeptUserList> deptUserLists = deptUserListss.get(position);

        if (null !=subDeptLists){

            List<List<DeptUserResponse.SubDeptList>> subDeptListssnext = new ArrayList<>();
            List<List<DeptUserResponse.DeptUserList>> deptUserListssnext =new ArrayList<>();
            for (int i=0;i <subDeptLists.size();i++){

                DepartmentGroup  departmentGroup=new DepartmentGroup();
                departmentGroup.setDeparemtname(subDeptLists.get(i).getName());
                departmentGroup.setSub(true);
                departmentGroup.setIschecked(isChecked);

                if (null !=subDeptLists){

                    subDeptListssnext.add(subDeptLists.get(i).getSubDeptList());
                    deptUserListssnext.add(subDeptLists.get(i).getDeptUserList());
                    departmentGroup.setDeptUserListss(deptUserListssnext);
                    departmentGroup.setSubDeptListss(subDeptListssnext);
                }
                departmentGroupList.add(departmentGroup);

            }
        }
        if (null !=deptUserLists){

            for (int i=0;i <deptUserLists.size();i++){

                DepartmentGroup  departmentGroup=new DepartmentGroup();
                departmentGroup.setDeparemtname(deptUserLists.get(i).getName());
                departmentGroup.setSub(false);
                departmentGroup.setIschecked(isChecked);
                departmentGroupList.add(departmentGroup);

            }
        }
        return  departmentGroupList;

    }

}
